const CommomUtil = require('../utils/CommomUtil')
const DownloadUtil = require('../utils/Download')
const UpLoadUtil = require('../utils/COSUpLoad')
const redis = require('../config').redis_test;
const uuid = require('uuid')
const exec = require('child_process').execSync
const fs = require("fs")
const Path = require("path")
const convert_dao = require('../dao/convert_dao')

module.exports.convert2img = convert2img
async function convert2img(ctx) {
    let resData = {}
    //#从redis队列里获取一条记录
    let data = JSON.parse(await redis.lpop('obj2img'));
    if (CommomUtil.isEmpty(data)) {
        resData['ret'] = false
        resData['msg'] = '参数不全!'
        return resData
        // return ctx.body = resData
    }
    let { file_url, course_id } = data;
    let dir = uuid.v1();
    DownloadUtil.downloadFile(file_url, dir, async function () {
        console.log('= = = = = = = = = = 转换开始 = = = = = = = = =\n' + new Date())
        let filename = Path.basename(file_url)
        let expdname = Path.extname(file_url)
        let basename = Path.basename(filename, expdname)
        let sourcePath = Path.format({
            dir: './static/file/' + dir,
            base: filename
        })
        let convertPath = Path.format({
            dir: './static/pdf/' + dir,
            base: basename + '.pdf'
        })
        let outfilePath = Path.format({
            dir: './static/img/' + dir,
            base: basename + '.png'
        })
        switch (expdname) {
            case '.pdf':
                let pdf2image = 'convert ' + sourcePath + ' ' + outfilePath
                exec(pdf2image, function (err, stdout, stderr) {
                    if (err) {
                        console.log(err)
                    } else {
                        console.log("success trans from pdf to image!\r\n")
                    }
                })
                break;
            case '.doc':
            case '.docx':
            case '.ppt':
            case '.pptx':
            case '.xls':
            case '.xlsx':
                let comd = {
                    obj2pdf: 'soffice --headless --convert-to pdf ' + sourcePath + ' --outdir ./static/pdf/' + dir, //ppt to pdf 
                    pdf2image: 'convert ' + convertPath + ' ' + outfilePath //pdf to png
                }
                exec(comd.obj2pdf, function (err, stdout, stderr) {
                    if (err) {
                        console.log(err)
                        return ctx.body = resData;
                    } else {
                        console.log("translating from ppt to pdf!\r\n")
                        //# step2:trans pdf to image
                    }
                })
                exec(comd.pdf2image, function (err, stdout, stderr) {
                    if (err) {
                        console.log(err)
                        break;
                    } else {
                        console.log("success trans from pdf to image!\r\n")
                    }
                })
                break;
            default: break;
        }
        console.log(new Date() + '\n= = = = = = = = = = 转换完成 = = = = = = = = =')
        //#上传图片
        let file_list = fs.readdirSync("./static/img/" + dir)
        let image_url = await UpLoadUtil.uploadFile(file_list, dir);
        //#修改数据库记录
        convert_dao.update_url(course_id, image_url, file_list.length);
    })
    resData.ret = 'ok!'
    return resData
    // return ctx.body = resData
}

